package optional.code;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Description: 好的示例演示
 *
 * @author code-worker
 * @date 2021/08/25 7:32
 */
public class GoodPracticeJdk8 {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName(String methodName) {
        System.out.print(methodName + "method called\n");
        return methodName;
    }

    @Override
    public String toString() {
        return "GoodPracticeJdk8{" +
                "name='" + name + '\'' +
                '}';
    }

    /**
     * task: 对比 OrElse | OrElseGet
     * @date 2021/8/28 10:01
     */
    public void compareOrElse_OrElseGet(){
        //output: method called
        //即方法不为空，orElse也会执行
        String name1 = Optional.of("String").orElse(getName("compareOrElse_OrElseGet : orElse "));
        //output:
        String name2 = Optional.of("String").orElseGet(()->getName("compareOrElse_OrElseGet : orElseGet "));
    }

    /**
     * task: 示例 OrElseThrow
     * @date 2021/8/28 10:02
     */
    public void checkNPE() throws NullPointerException{
        String s = null;
        Optional.ofNullable(s).orElseThrow(() ->new NullPointerException("没有数据"));
    }

    /**
     * task: 对比: isPresent | ifPresent
     * @date 2021/8/28 10:02
     */
    public void ifPresent_isPresent(){
        String s = null;
        //其实如果这样使用isPresent有点鸡肋，直接判空不香吗
        if(Optional.ofNullable(s).isPresent()){
            //非空的情况下执行
            s = getName("ifPresent_isPresent: isPresent ");
            System.out.println(s);
        }
        //非空的情况下执行ifPresent
        Optional.ofNullable(s).ifPresent(v -> {
            v=getName("ifPresent_isPresent: ifPresent ");
            System.out.println(v + "1");
        });
    }

    /**
     * task: 示例: isPresent
     * @date 2021/8/28 10:03
     */
    public void isPresentUsage(){
        List<String> list = new ArrayList<>(2);
        list.add("hello");
        list.add("world");
        System.out.println(list.stream().findAny().isPresent());
    }

    /**
     * task: 示例: map
     * @date 2021/8/28 10:09
     */
    public void mapUsage(){
        GoodPracticeJdk8 demo = new GoodPracticeJdk8();
        //非空的情况下执行map，获取demo的getName
        System.out.println(Optional.of(demo).map(v -> v.getName("mapUsage ")));
    }

    /**
     * task: 示例: filter
     * @date 2021/8/28 10:39
     */
    public void filterUsage() {
        GoodPracticeJdk8 demo = new GoodPracticeJdk8();
        demo.setName("Test");
        //因为符合条件，所以返回整个demo
        System.out.println("filterUsage "+Optional.of(demo).filter(v -> v.getName()!=null));
    }

    public static void main(String[] args) {
        GoodPracticeJdk8 demo = new GoodPracticeJdk8();
        demo.compareOrElse_OrElseGet();
        try{
            demo.checkNPE();
        }catch (NullPointerException e){
            System.out.println("checkNPE 抛出了异常: "+e.getLocalizedMessage());
        }
        demo.ifPresent_isPresent();
        demo.isPresentUsage();
        demo.mapUsage();
        demo.filterUsage();
    }

}
